﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;
namespace AlbonWeb.Order
{
    public partial class OrderPage : System.Web.UI.Page
    {
        BLL.OrderBLL orderbll = new BLL.OrderBLL();
        BLL.StyleBLL stylebll = new BLL.StyleBLL();
        private string orderitemid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["orderid"] != null)
                {
                    //绑定Order Page信息
                    initOrder(Request["orderid"].ToString());

                }
            }
        }
        protected void initOrder(string orderid)
        {
            DataTable dt = orderbll.getOrderByID(orderid);
            //绑定Cover Page
            clientName1.Text = dt.Rows[0]["clientName"].ToString();
            refNo1.Text = dt.Rows[0]["refNo"].ToString();
            userName1.Text = dt.Rows[0]["userName"].ToString().ToUpper();
            previousOrder.Text = orderbll.getPreviousOrderRef(orderid);
            if (dt.Rows[0]["requiredShipDate"].ToString() != "")
            {
                shipDate.Text = DateTime.Parse(dt.Rows[0]["requiredShipDate"].ToString()).ToString("MM-dd-yyyy");
            }
            //绑定布料信息
            string cmdstr = "select * from ClothBuyView where orderRefNo='"+refNo1.Text+"' and statusID!=1";
            SqlHelper.BindRepeater(repCloth, SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
            //绑定订单信息
            clientName.Text = dt.Rows[0]["clientName"].ToString();
            refNo.Text = dt.Rows[0]["refNo"].ToString();
            userName.Text = dt.Rows[0]["userName"].ToString();
            invoiceNo.Text = (dt.Rows[0]["invoiceNo"].ToString() == "") ? "No Invoice" : dt.Rows[0]["invoiceNo"].ToString();
            createDate.Text = (DateTime.Parse(dt.Rows[0]["createDate"].ToString())).ToString("yyyy/MM/dd hh:mm:ss");
            //绑定订单项
            cmdstr = "select * from OrderItemView where finished=1 and orderid=" + orderid;
            bindRep(cmdstr, repItem);
        }


        //绑定订单项列表
        protected void repItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                Repeater repStyleList = (Repeater)e.Item.FindControl("repStyleList");
                string stylelist = ((HiddenField)e.Item.FindControl("hfstylelist")).Value;
                orderitemid = ((HiddenField)e.Item.FindControl("hforderitemid")).Value;
                string cmdstr = "select ID,styleName from Style where ID in (" + stylelist + ")";
                bindRep(cmdstr, repStyleList);
            }
        }
        //绑定尺寸和样式
        protected void repStyleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                string styleid = ((HiddenField)e.Item.FindControl("hfstyleid")).Value;
                Repeater repSize = (Repeater)e.Item.FindControl("repSize");
                Repeater repStyle = (Repeater)e.Item.FindControl("repStyle");
                Label remark = (Label)e.Item.FindControl("remark");
                string cmdstr = "select * from ItemStyleView where orderItemID=" + orderitemid + " and styleID=" + styleid;
                bindRep(cmdstr, repStyle);
                cmdstr = "select * from ItemSizeView where orderItemID=" + orderitemid + " and styleID=" + styleid;
                bindRep(cmdstr, repSize);
                BLL.ItemStyleBLL itemstylebll = new BLL.ItemStyleBLL();
                DataTable dt = itemstylebll.getStyleRemark(orderitemid, styleid);
                if (dt.Rows.Count > 0)
                {
                    remark.Text = dt.Rows[0]["remark"].ToString();
                }
            }
        }

        //绑定repeater
        protected void bindRep(string cmdstr, Repeater rep)
        {
            SqlHelper.BindRepeater(rep, SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }
    }
}